package springSex.testDao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import springSex.JDBC;
import springSex.testDao.TestDao;
import springSex.userEntity.UserEntity;
@Repository
public class TestDaoImpl implements TestDao{
	@Autowired
	private JDBC jdbc;
	@Override
	public List<UserEntity> listAll() {
		Connection conn = jdbc.getConn();
		String sql = "select id,user_name,pass_word from user_spring";
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<UserEntity> list = new ArrayList();
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				int i = 1;
				Integer id = rs.getInt(i++);
				String userName = rs.getString(i++);
				String password = rs.getString(i++);
				list.add(new UserEntity(id, userName,password));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		JDBC.close(null, ps, conn);
		return list;
	}

}
